﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace KDSugar.Galaxy.API.View
{
    /// <summary>
    /// 单据查看指令
    /// </summary>
    public class ViewCommand : Base.BaseNumberId
    {
        /// <summary>
        /// 单据查看指令URL
        /// </summary>
        public const string VIEW_COMMAND_URL = "/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc";

        /// <summary>
        /// 单据查看指令初始化
        /// </summary>
        /// <param name="_formId"></param>
        /// <param name="_client"></param>
        public ViewCommand(string _formId, GalaxyCloudAPIClient _client)
            : base(VIEW_COMMAND_URL, _formId, _client)
        {

        }

        /// <summary>
        /// 验证查看单据执行返回数据是否符合规则
        /// </summary>
        /// <param name="_responseData"></param>
        /// <returns></returns>
        public override bool ValidateResponseDataRule(string _responseData)
        {
            //bool result = false;
            //try
            //{
            //    if (!string.IsNullOrWhiteSpace(_responseData))
            //    {
            //        ViewReturnInfo returnInfo = JsonConvert.DeserializeObject<ViewReturnInfo>(_responseData);
            //        if (returnInfo != null)
            //        {
            //            var r1 = returnInfo.Result;
            //            if (r1 != null)
            //            {
            //                var r2 = r1.ResponseStatus;
            //                if ((r2 == null && r1.Result != null) || 
            //                    (r2 != null && r2.IsSuccess))
            //                    result = true;
            //            }
            //        }
            //    }
            //}
            //catch
            //{
            //    result = false;
            //}
            //return result;
            return true;
        }

        /// <summary>
        /// 构建请求内容
        /// </summary>
        /// <returns></returns>
        public override string BuildRequestContent()
        {
            return BuildRequestContentByAppendFormId();
        }

        /// <summary>
        /// 执行单据查询指令
        /// </summary>
        /// <returns></returns>
        public Dictionary<string,object> ExecuteViewBill()
        {
            Dictionary<string, object> result = null;
            string errorMsg = "未知错误";
            var resultPack = APIClient.ExecuteCommand(this);
            bool isSuccess = false;
            try
            {
                if (resultPack != null)
                {
                    errorMsg = resultPack.ResponseData;
                    if (resultPack.IsResponseSuccess)
                    {
                        errorMsg = "返回数据JSON反序列化为对象失败";
                        ViewReturnInfo returnInfo = JsonConvert.DeserializeObject<ViewReturnInfo>(resultPack.ResponseData);
                        if (returnInfo != null)
                        {
                            var r = returnInfo.Result;
                            if (r != null)
                            {
                                var r1 = r.Result;
                                if (r1 != null)
                                {
                                    isSuccess = true;
                                    result = r1;
                                }
                                else
                                {
                                    errorMsg = "单据字段信息（Result）为NULL";
                                    throw new Exception(errorMsg);
                                }
                            }
                            else
                            {
                                errorMsg = "返回信息Result为NULL";
                                throw new Exception(errorMsg);
                            }
                        }
                        else
                        {
                            errorMsg = "反序列化为对象后对象为NULL";
                            throw new Exception(errorMsg);
                        }
                    }
                    else
                    {
                        errorMsg = "返回数据【IsResponseSuccess】为false";
                        throw new Exception(errorMsg);
                    }
                }
                else
                {
                    errorMsg = "没有返回数据";
                    throw new Exception(errorMsg);
                }
            }
            catch(Except.ExecuteException ex)
            {
                ex.OperateCommandName = GetOperateCommandName();
                ex.AppendMessage = errorMsg;
                ex.ExecuteData = resultPack;
                throw ex;
            }
            if (!isSuccess)
                throw new Exception(errorMsg);
            return result;
        }
    }
}
